/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.tsunami.plugins.detectors.directorytraversal.genericpathtraversaldetector;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.testing.EqualsTester;
import com.google.tsunami.common.net.http.HttpRequest;
import com.google.tsunami.proto.NetworkService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link PotentialExploit}. */
@RunWith(JUnit4.class)
public final class PotentialExploitTest {
  private static final NetworkService MINIMAL_NETWORK_SERVICE =
      NetworkService.newBuilder().setServiceName("http").build();

  private static final HttpRequest BASIC_GET_REQUEST =
      HttpRequest.get("https://google.com").withEmptyHeaders().build();

  @Test
  public void equals_always_passesEqualityTester() {
    HttpRequest httpRequestWithDifferentHost =
        HttpRequest.get("https://youtube.com").withEmptyHeaders().build();
    HttpRequest httpRequestWithAdditionalGetParameters =
        HttpRequest.get("https://google.com?a=b").withEmptyHeaders().build();

    new EqualsTester()
        .addEqualityGroup(
            PotentialExploit.create(
                MINIMAL_NETWORK_SERVICE, BASIC_GET_REQUEST, "", PotentialExploit.Priority.LOW),
            PotentialExploit.create(
                MINIMAL_NETWORK_SERVICE,
                BASIC_GET_REQUEST,
                "ignored",
                PotentialExploit.Priority.LOW),
            PotentialExploit.create(
                NetworkService.newBuilder().setServiceName("ssh").build(),
                BASIC_GET_REQUEST,
                "",
                PotentialExploit.Priority.LOW),
            PotentialExploit.create(
                MINIMAL_NETWORK_SERVICE, BASIC_GET_REQUEST, "", PotentialExploit.Priority.HIGH))
        .addEqualityGroup(
            PotentialExploit.create(
                MINIMAL_NETWORK_SERVICE,
                httpRequestWithDifferentHost,
                "",
                PotentialExploit.Priority.LOW))
        .addEqualityGroup(
            PotentialExploit.create(
                MINIMAL_NETWORK_SERVICE,
                httpRequestWithAdditionalGetParameters,
                "",
                PotentialExploit.Priority.LOW))
        .testEquals();
  }

  @Test
  public void hashCode_always_isEqualToRequestHashCode() {
    PotentialExploit exploit =
        PotentialExploit.create(
            MINIMAL_NETWORK_SERVICE, BASIC_GET_REQUEST, "", PotentialExploit.Priority.LOW);

    assertThat(exploit.hashCode()).isEqualTo(BASIC_GET_REQUEST.hashCode());
  }

  @Test
  public void toString_always_addsInformationAboutRequest() {
    String stringifiedExploit =
        PotentialExploit.create(
                MINIMAL_NETWORK_SERVICE, BASIC_GET_REQUEST, "test", PotentialExploit.Priority.LOW)
            .toString();

    assertThat(stringifiedExploit).contains(BASIC_GET_REQUEST.toString());
  }

  @Test
  public void toString_always_containsPayload() {
    String stringifiedExploit =
        PotentialExploit.create(
                MINIMAL_NETWORK_SERVICE, BASIC_GET_REQUEST, "test", PotentialExploit.Priority.LOW)
            .toString();

    assertThat(stringifiedExploit).contains("test");
  }
}
